
package com.example.medical_delivery_system.service.impl;

import com.example.medical_delivery_system.common.Exception;
import com.example.medical_delivery_system.common.ServiceResultEnum;
import com.example.medical_delivery_system.dao.UserMapper;
import com.example.medical_delivery_system.dao.TokenMapper;
import com.example.medical_delivery_system.entity.User;
import com.example.medical_delivery_system.entity.Token;
import com.example.medical_delivery_system.service.UserService;
import com.example.medical_delivery_system.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.medical_delivery_system.api.param.UpdatePasswordParam;
import com.example.medical_delivery_system.api.param.UpdateNameParam;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TokenMapper tokenMapper;

    @Override
    public String register(String registerName, String registerPasswd) {
        if (userMapper.selectByLoginName(registerName) != null) {
            return ServiceResultEnum.SAME_LOGIN_NAME_EXIST.getResult();
        }
        User registerUser = new User();
        registerUser.setUName(registerName);
        registerUser.setUPasswd(registerPasswd);
        if (userMapper.insertSelective(registerUser) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String login(String loginName, String userPasswd) {
        User loginUser = userMapper.selectByLoginNameAndPasswd(loginName, userPasswd);
        if (loginUser != null) {
            //登录后即执行修改token的操作
            String token = getNewToken(System.currentTimeMillis() + "", loginUser.getUId());
            Token token1 = tokenMapper.selectByPrimaryKey(loginUser.getUId());
            //当前时间
            Date now = new Date();
            //过期时间
            Date expireTime = new Date(now.getTime() + 2 * 24 * 3600 * 1000);//过期时间 48 小时
            if (token1 == null) {
                token1 = new Token();
                token1.setUId(loginUser.getUId());
                token1.setToken(token);
                token1.setUpdateTime(now);
                token1.setExpireTime(expireTime);
                //新增一条token数据
                if (tokenMapper.insertSelective(token1) > 0) {
                    //新增成功后返回
                    return token;
                }
            } else {
                token1.setToken(token);
                token1.setUpdateTime(now);
                token1.setExpireTime(expireTime);
                //更新
                if (tokenMapper.updateByPrimaryKeySelective(token1) > 0) {
                    //修改成功后返回
                    return token;
                }
            }

        }
        return ServiceResultEnum.LOGIN_ERROR.getResult();
    }

    /**
     * 获取token值
     *
     * @param timeStr
     * @param userId
     * @return
     */
    private String getNewToken(String timeStr, Long userId) {
        String src = timeStr + userId + NumberUtil.genRandomNum(6);
        return SystemUtil.genToken(src);
    }





    @Override
    public Boolean updatePassword(Long uId, String originalPasswd, String newPasswd) {
        User user = userMapper.selectByPrimaryKey(uId);
        //当前用户非空才可以进行更改
        if (user != null) {
            //比较原密码是否正确
            if (originalPasswd.equals(user.getUPasswd())) {
                //设置新密码并修改
                user.setUPasswd(newPasswd);
                if (userMapper.updateByPrimaryKeySelective(user) > 0 && tokenMapper.deleteByPrimaryKey(uId) > 0) {
                    //修改成功且清空当前token则返回true
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public Boolean updateName(Long uId, String newName) {
        User user = userMapper.selectByPrimaryKey(uId);
        //当前用户非空才可以进行更改
        if (user != null) {
            //设置新名称并修改
            user.setUName(newName);
            if (userMapper.updateByPrimaryKeySelective(user) > 0) {
                //修改成功则返回true
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean logout(Long userId) {
        return tokenMapper.deleteByPrimaryKey(userId) > 0;
    }


}